
using System;
using System.Data;

namespace FileHelpers.DataLink
{
#if !NETSTANDARD2_0
	using System.Data.SqlClient;

	/// <summary>
	/// This is a base class that implements the <see cref="DataStorage"/> for
	/// Microsoft SqlServer.
	/// </summary>
	public sealed class SqlServerStorage : DatabaseStorage
    {
	#region "  Constructors  "

        /// <summary>
        /// Create a new instance of the SqlServerStorage based on the record
        /// type provided.
        /// </summary>
        /// <param name="recordType">The type of the record class.</param>
        public SqlServerStorage(Type recordType)
            : this(recordType, string.Empty) {}

        /// <summary>
        /// Create a new instance of the SqlServerStorage based on the record
        /// type provided.
        /// </summary>
        /// <param name="recordType">The type of the record class.</param>
        /// <param name="connectionStr">
        /// The full connection string used to connect to the SQL server.
        /// </param>
        public SqlServerStorage(Type recordType, string connectionStr)
            : base(recordType)
        {
            ConnectionString = connectionStr;
        }

        /// <summary>
        /// Create a new instance of the SqlServerStorage based on the record
        /// type provided (uses windows auth)
        /// </summary>
        /// <param name="recordType">The type of the record class.</param>
        /// <param name="server">The server name or IP of the sqlserver</param>
        /// <param name="database">The database name into the server.</param>
        public SqlServerStorage(Type recordType, string server, string database)
            : this(recordType, server, database, string.Empty, string.Empty) {}

        /// <summary>
        /// Create a new instance of the SqlServerStorage based on the record
        /// type provided (uses SqlServer auth)
        /// </summary>
        /// <param name="recordType">The type of the record class.</param>
        /// <param name="server">The server name or IP of the sqlserver</param>
        /// <param name="database">The database name into the server.</param>
        /// <param name="user">The SQL username to login into the server.</param>
        /// <param name="pass">The pass of the SQL username to login into the server.</param>
        public SqlServerStorage(Type recordType, string server, string database, string user, string pass)
            : this(recordType, DataBaseHelper.SqlConnectionString(server, database, user, pass))
        {
            mServerName = server;
            mDatabaseName = database;
            mUserName = user;
            mUserPass = pass;
        }

	#endregion

	#region "  Create Connection and Command  "

        /// <summary>Create a connection object to the database</summary>
        /// <returns>SQL server Connection Object.</returns>
        protected override sealed IDbConnection CreateConnection()
        {
            string conString;
            if (ConnectionString == string.Empty) {
                if (mServerName == null ||
                    mServerName == string.Empty)
                    throw new BadUsageException("The ServerName can't be null or empty.");

                if (mDatabaseName == null ||
                    mDatabaseName == string.Empty)
                    throw new BadUsageException("The DatabaseName can't be null or empty.");

                conString = DataBaseHelper.SqlConnectionString(ServerName, DatabaseName, UserName, UserPass);
            }
            else
                conString = ConnectionString;

            return new SqlConnection(conString);
        }

	#endregion

	#region "  ServerName  "

        private string mServerName = string.Empty;

        /// <summary> The server name or IP of the SqlServer </summary>
        public string ServerName
        {
            get { return mServerName; }
            set
            {
                mServerName = value;
                ConnectionString = DataBaseHelper.SqlConnectionString(ServerName, DatabaseName, UserName, UserPass);
            }
        }

	#endregion

	#region "  DatabaseName  "

        private string mDatabaseName = string.Empty;

        /// <summary> The name of the database. </summary> 
        public string DatabaseName
        {
            get { return mDatabaseName; }
            set
            {
                mDatabaseName = value;
                ConnectionString = DataBaseHelper.SqlConnectionString(ServerName, DatabaseName, UserName, UserPass);
            }
        }

	#endregion

	#region "  UserName  "

        private string mUserName = string.Empty;

        /// <summary> The user name used to logon into the SqlServer. (leave empty for WindowsAuth)</summary>
        public string UserName
        {
            get { return mUserName; }
            set
            {
                mUserName = value;
                ConnectionString = DataBaseHelper.SqlConnectionString(ServerName, DatabaseName, UserName, UserPass);
            }
        }

	#endregion

	#region "  UserPass  "

        private string mUserPass = string.Empty;

        /// <summary> The user pass used to logon into the SqlServer. (leave empty for WindowsAuth)</summary>
        public string UserPass
        {
            get { return mUserPass; }
            set
            {
                mUserPass = value;
                ConnectionString = DataBaseHelper.SqlConnectionString(ServerName, DatabaseName, UserName, UserPass);
            }
        }

	#endregion

	#region "  ExecuteInBatch  "

        /// <summary>do we get groups of records or singularly</summary>
        protected override bool ExecuteInBatch
        {
            get { return true; }
        }

	#endregion
    }
#endif
}